package com.margo.project.append.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.margo.project.append.dao.MarColumnNameDAO;
import com.margo.project.append.model.MarColumnNameVO;

@Service
public class MarColumnNameService {
	@Autowired
	private MarColumnNameDAO marColumnNameDAO;
	
	public void createVO(String tableName,String packageName){
		
		String filePath=MarColumnNameService.class.getResource("/java/").getPath();
		List<MarColumnNameVO> lstItem=marColumnNameDAO.queryMarColumnNameList(tableName);
		Writer out=null;
		BufferedWriter bw=null;
		StringBuilder sb=new StringBuilder();
		sb.append("import ").append(packageName).append("; \r");
		sb.append(" \r");
		String className=initFiledName(tableName.substring(5));
		className="Sw"+className+"VO";
		sb.append("public class ").append(className).append("{").append(" \r");
		for(MarColumnNameVO marColumnNameVO:lstItem) {
			String filedName=initFiledName(marColumnNameVO.getColumnName());
			marColumnNameVO.setFiledName(initFiledName(marColumnNameVO.getColumnName()));
			sb.append("\t").append("//")
					.append(marColumnNameVO.getDataType())
					.append(marColumnNameVO.getComments()).append(" \r");
			sb.append("\t").append("@Column(name = ").append("\"").append(marColumnNameVO.getColumnName()).append("\"").append(")\r");
			if(marColumnNameVO.getDataType().equals("VARCHAR2")) {
				sb.append("\t").append("private ").append("String ").append(filedName).append("; \r");
			}else if(marColumnNameVO.getDataType().equals("NUMBER")) {
				sb.append("\t").append("private ").append("Integer ").append(filedName).append("; \r");
			}else if(marColumnNameVO.getDataType().equals("TIMESTAMP(6)")) {
				sb.append("\t").append("private ").append("Date ").append(filedName).append("; \r");
			}
		}
		for(MarColumnNameVO marColumnNameVO:lstItem) {
			String fieldName=initFiledName(marColumnNameVO.getColumnName());
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			String setter = "set" + firstLetter + fieldName.substring(1);
			if(marColumnNameVO.getDataType().equals("VARCHAR2")) {
				sb.append("\t").append("public String ").append(getter).append("(){ \r");
				sb.append("\t").append("\t").append("return this.").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
				sb.append("\t").append("public void ").append(setter).append("(String ").append(fieldName).append("){ \r");
				sb.append("\t").append("\t").append("this.").append(fieldName).append("=").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
			}else if(marColumnNameVO.getDataType().equals("NUMBER")) {
				sb.append("\t").append("public Integer ").append(getter).append("(){ \r");
				sb.append("\t").append("\t").append("return this.").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
				sb.append("\t").append("public void ").append(setter).append("(Integer ").append(fieldName).append("){ \r");
				sb.append("\t").append("\t").append("this.").append(fieldName).append("=").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
			}else if(marColumnNameVO.getDataType().equals("TIMESTAMP(6)")) {
				sb.append("\t").append("public Date ").append(getter).append("(){ \r");
				sb.append("\t").append("\t").append("return this.").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
				sb.append("\t").append("public void ").append(setter).append("(Date ").append(fieldName).append("){ \r");
				sb.append("\t").append("\t").append("this.").append(fieldName).append("=").append(fieldName).append("; \r");
				sb.append("\t").append("}");
				sb.append(" \r");
			}
		}
		sb.append("}");
		try {
			File file=new File(filePath+className+".java");
			out=new FileWriter(file);
			bw=new BufferedWriter(out);
			bw.write(sb.toString());
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(out !=null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(bw !=null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String initFiledName(String columnName) {
		System.out.println(columnName);
		StringBuilder column=new StringBuilder(columnName);
		StringBuilder filedName=new StringBuilder();
		int j=-1;
		for(int i=0;i<column.length();i++) {
			System.out.println(column.substring(i, i+1));
			if(column.substring(i, i+1).equals("_")) {
				j=i+1;
			}else {
				if(j==i) {
					filedName.append(column.charAt(i));
				}else {
					filedName.append(columnName.toLowerCase().charAt(i));
				}
				
			}
			
		}
		return filedName.toString();
	}
}
